﻿using System.Collections.Generic;
using System.Reflection;

namespace Ast.Core.Services.Extensions
{
    public static class DynamicValueAccessorExtensions
    {
        public static IDictionary<FieldInfo, object> GetObjectFieldValues(this IDynamicValueAccessor dynamicValueAccessor, object obj)
        {
            var retVal = new Dictionary<FieldInfo, object>();
            var fieldInfos = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
            foreach (var fieldInfo in fieldInfos)
            {
                var val = dynamicValueAccessor.GetValue(obj, fieldInfo.Name);
                retVal.Add(fieldInfo, val);
            }
            return retVal;
        }

        public static IDictionary<PropertyInfo, object> GetObjectPropertyValues(this IDynamicValueAccessor dynamicValueAccessor, object obj)
        {
            var retVal = new Dictionary<PropertyInfo, object>();
            var propertyInfos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (var propertyInfo in propertyInfos)
            {
                var val = dynamicValueAccessor.GetValue(obj, propertyInfo.Name);
                retVal.Add(propertyInfo, val);
            }
            return retVal;
        }
    }
}